package com.solo.eggsolo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.solo.eggsolo.web.interceptor.JwtInterceptor;

/**
 * 
 * @author Yu
 *
 */
@Configuration
public class MyHandlers extends WebMvcConfigurationSupport {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		/**
		 * 解决静态资源无法访问
		 */
		registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
		/**
		 * 解决swagger无法访问
		 */
		registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		/**
		 *解决swagger的js文件无法访问
		 */
		
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
//	registry.addResourceHandler("doc.html")
//	.addResourceLocations("classpath:/META-INF/resources/");
	}

	@Autowired
	private JwtInterceptor jwtInterceptor;

	/**
	 * 添加jwt拦截器
	 */

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(jwtInterceptor).addPathPatterns("/auth/**");
	}

	/**
	 * 防止跨域请求
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**").allowedMethods("POST", "GET", "PUT", "DELETE", "OPTIONS").allowedHeaders("*")
				.allowedOrigins("*").maxAge(3600);
	}

}
